package com.one.service.impl;

import com.one.dto.PaginationDate;
import com.one.dto.Result;
import com.one.dto.ReturnDate;
import com.one.entity.SysCollegeEntity;
import com.one.entity.SysUserEntity;
import com.one.entity.UserRoleEntity;
import com.one.mapper.SysCollegeMapper;
import com.one.service.SysCollegeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.utils.PaginationUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-10-05
 */
@Service
public class SysCollegeServiceImpl extends ServiceImpl<SysCollegeMapper, SysCollegeEntity> implements SysCollegeService {
    private final SysCollegeMapper sysCollegeMapper;

    public SysCollegeServiceImpl(SysCollegeMapper sysCollegeMapper) {
        this.sysCollegeMapper = sysCollegeMapper;
    }
    //增加学院
    @Override
    public Result insertCollege(SysCollegeEntity sysCollegeEntity) {
        return Result.ok().data(sysCollegeMapper.insertCollege(sysCollegeEntity));
    }
    //根据学院标号和名称查询学院
    @Override
    public Result selectCollegeByCodeAndName(SysCollegeEntity sysCollegeEntity) {
        if (sysCollegeEntity.getCode().equals("") && sysCollegeEntity.getName().equals("")) {
            return Result.error().message("编号和名称不能都为空");
        }
        return Result.ok().data(sysCollegeMapper.selectCollegeByCodeAndName(sysCollegeEntity));
    }
    //删除学院
    @Override
    public Result deleteCollege(SysCollegeEntity sysCollegeEntity) {
        return Result.ok().data(sysCollegeMapper.deleteCollege(sysCollegeEntity));
    }
    //修改学院的信息
    @Override
    public Result updateCollege(SysCollegeEntity sysCollegeEntity) {
        return Result.ok().data(sysCollegeMapper.updateCollege(sysCollegeEntity));
    }
    //查询学院的信息
    @Override
    public Result selectCollege(SysCollegeEntity sysCollegeEntity) {
        int page = sysCollegeEntity.getPage();
        sysCollegeEntity.setPage(PaginationUtils.getPageIndex(sysCollegeEntity.getPage(), sysCollegeEntity.getNumber()));
        List<Object> result=sysCollegeMapper.selectCollege(sysCollegeEntity);
        int sum=((List<Integer>) result.get(1)).get(0);
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(sysCollegeEntity.getNumber(), page, sum);
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        return Result.ok().data(new ReturnDate(paginationDate,(List<SysCollegeEntity>) result.get(0) ));
    }
    //获取责任人的id
    /*public List<UserRoleEntity> getCeachId(){
        return sysCollegeMapper.getCeachId();
    }*/
    //获取责任人
    @Override
    public Result getCeachCode() {
        List<UserRoleEntity> CeachId=sysCollegeMapper.getCeachId();
        if (CeachId.size()==0){
            return Result.error().message("没有管理员");
        }
        String ci="";
        for (UserRoleEntity CI:CeachId){
            ci+=CI.getUserId()+',';
        }
        SysUserEntity sysUserEntity=new SysUserEntity();
        sysUserEntity.setName(ci);
        return Result.ok().data(sysCollegeMapper.getCeachCode(sysUserEntity));
    }






}
